package com.asj.vivo.lock.service.recover.correct;

import static com.asj.enums.AppIdentifierDictEnums.supervision_NotSave;
import static com.asj.enums.AppIdentifierDictEnums.supervision_cancel;
import static com.asj.enums.DeviceIdentifierDictEnums.CANCEL_SUPERVISE;
import static com.asj.enums.DeviceIdentifierDictEnums.LOCKED;
import static com.asj.enums.DeviceIdentifierDictEnums.ON_LINE;
import static com.asj.enums.DeviceIdentifierDictEnums.UNLOCKED;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockPassword;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockPhone;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockRemarks;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockStatus;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_resultMsg;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_superviseStatus;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;

import com.asj.MainActivityService;
import com.asj.service.oppoSdk.OPPOControlService;
import com.asj.utils.CommonsUtils;
import com.google.gson.JsonObject;
/**
 * 矫正设备状态信息
 * */
public class CorrectDeviceStatusInfo {
    //设备被锁定时的矫正处理
    public static void correctLockedStatus(JsonObject deviceLockStatus,
                                           JsonObject requestParam,
                                           OPPOControlService oppoControlService){
        /* 设置返回值Value为锁定时的操作 */
        requestParam.addProperty(json_key_lockStatus.getValue(),LOCKED.getValue());
        try {
            //获取操作设置参数
            String lockPhone = deviceLockStatus.get(json_key_lockPhone.getValue()).getAsString();
            String lockRemarks = deviceLockStatus.get(json_key_lockRemarks.getValue()).getAsString();
            String lockPassword = deviceLockStatus.get(json_key_lockPassword.getValue()).getAsString();

            /* 锁定操作 */
            oppoControlService.lockMachine(lockPassword,lockPhone,lockRemarks);

            setResultMsg(requestParam,LOCKED.getValue() + ":" + result_success.getMessage());
        }catch (Exception e){
            setResultMsg(requestParam,LOCKED.getValue() + ":" + e.getMessage());
        }
    }

    //设备未锁定时的矫正处理
    public static void correctUnlockedStatus(JsonObject requestParam,
                                             OPPOControlService oppoControlService){
        /* 设置返回值Value为解锁时的操作 */
        requestParam.addProperty(json_key_lockStatus.getValue(),UNLOCKED.getValue());
        try {
            oppoControlService.unlockMachine(true);

            setResultMsg(requestParam,UNLOCKED.getValue()+":"+ result_success.getMessage());
        }catch (Exception e){
            setResultMsg(requestParam,UNLOCKED.getValue()+":"+e.getMessage());
        }
    }

    //设备离线时矫正处理
    public static void correctOffLine(JsonObject requestParam) {
        /* 设置返回值Value为在线时的操作 */
        requestParam.addProperty(json_key_superviseStatus.getValue(),ON_LINE.getValue());
        try {
            setResultMsg(requestParam,ON_LINE.getValue()+":"+ result_success.getMessage());
        }catch (Exception e){
            setResultMsg(requestParam,ON_LINE.getValue()+":"+e.getMessage());
        }
    }

    //设备待取消监管时矫正处理
    public static void correctPendingSupervise(JsonObject requestParam,
                                               OPPOControlService oppoControlService,
                                               Context context) {
        /* 设置返回值Value为取消监管时的操作 */
        requestParam.addProperty(json_key_superviseStatus.getValue(),CANCEL_SUPERVISE.getValue());
        try {
            //取消监管
            oppoControlService.initiateOrCancelSupervision(supervision_NotSave.getValue(),supervision_cancel.getValue());
            //清空所有黑名单
            oppoControlService.clearBlackList();
            //发送通知提示取消监管
            CommonsUtils.showNotification(context,"监管已取消","设备已移除监管状态！恢复正常情况！");
            //销毁常驻后台任务
            MainActivityService.stopMeService(context);

            setResultMsg(requestParam,CANCEL_SUPERVISE.getValue()+":"+ result_success.getMessage());
        }catch (Exception e){
            setResultMsg(requestParam,CANCEL_SUPERVISE.getValue()+":"+e.getMessage());
        }
    }

    public static void setResultMsg(JsonObject requestParam, String message){
        if(requestParam.get(json_key_resultMsg.getValue()) != null){
            requestParam.addProperty(json_key_resultMsg.getValue(),requestParam.get(json_key_resultMsg.getValue()) + "," + message);
        }
    }

}
